#include "employee.h"

using namespace std;

int main(int argc, char *argv[])
{
	bool xml = false;
	bool _file = false;
	bool _fileOut = false;
	bool error = false;
	if (argc == 2)
	{
		if (argv[1][0] == '1')
		{
			xml = true;
		}
		else if (argv[1][0] == '2')
		{
			xml = false;
		}
		else
			_file = true;
	}
	if (argc == 3)
	{
		if (argv[2][0] == '1')
		{
			xml = true;
		}
		else if (argv[2][0] == '2')
		{
			xml = false;
		}
		else
			_fileOut = true;
		_file = true;
	}
	if (argc == 4)
	{
		if (argv[3][0] == '1')
		{
			xml = true;
		}
		else if (argv[3][0] == '2')
		{
			xml = false;
		}
		_fileOut = true;
		_file = true;
	}
	Employee *emp = NULL;
	std::vector<Employee *> empVec;
	if (xml)
	{
		if (_file&&!_fileOut)
		{
			ifstream file(argv[1]);
			if (file.good())
				while(!file.eof())
				{
					try
					{
						emp = emp->fromXML(file);
					}
					catch(EmployeeError e)
					{
						std::cout << "\nAn error occoured: ";
						std::cout << e.what();
						error = true;
					}
					if ((emp != NULL)&&(!file.eof())&&!error)
					{
						emp->print();
						empVec.push_back(emp);
					}
					error = false;
				}
			if (file.good())
				for (size_t i = 0; i < empVec.size(); i ++)
					empVec[i]->write(cout);
		}
		else if (_file&&_fileOut)
		{
			ifstream file(argv[1]);
			if (file.good())
				while(!file.eof())
				{
					try
					{
						emp = emp->fromXML(file);
					}
					catch(EmployeeError e)
					{
						std::cout << "\nAn error occoured: ";
						std::cout << e.what();
						error = true;
					}
					if ((emp != NULL)&&(!file.eof())&&!error)
					{
						emp->print();
						empVec.push_back(emp);
					}
					error = false;
				}

			ofstream fileOut(argv[2]);
			if (fileOut.good())
				for (size_t i = 0; i < empVec.size(); i ++)
					empVec[i]->write(fileOut);
		}
	}
	else
	{
		if (_file&&!_fileOut)
		{
			ifstream file(argv[1]);
			if (file.good())
				while(!file.eof())
				{
					try
					{
						emp = emp->read(file);
					}
					catch(EmployeeError e)
					{
						std::cout << "\nAn error occoured: ";
						std::cout << e.what();
						error = true;
					}
					if ((emp != NULL)&&(!file.eof())&&!error)
					{
						emp->print();
						empVec.push_back(emp);
					}
					error = false;
				}

			if (file.good())
				for (size_t i = 0; i < empVec.size(); i ++)
					empVec[i]->toXML(cout);
		}
		else if (_file&&_fileOut)
		{
			ifstream file(argv[1]);
			if (file.good())
				while(!file.eof())
				{
					try
					{
						emp = emp->read(file);
					}
					catch(EmployeeError e)
					{
						std::cout << "\nAn error occoured: ";
						std::cout << e.what();
						error = true;
					}
					if ((emp != NULL)&&(!file.eof())&&!error)
					{
						emp->print();
						empVec.push_back(emp);
					}
					error = false;
				}

			ofstream fileOut(argv[2]);
			if (fileOut.good())
				for (size_t i = 0; i < empVec.size(); i ++)
					empVec[i]->toXML(fileOut);
		}
	}
	if (xml)
		system("Employee employeeTest.txt employeeTest.xml 2");
	return 0;
}